package com.qidian.k12.service.impl;

import com.qidian.k12.dto.PageInfo;
import com.qidian.k12.pojo.Admin;
import com.qidian.k12.mapper.AdminMapper;
import com.qidian.k12.service.AdminService;
import com.qidian.k12.util.JsonTools;
import com.qidian.k12.util.K12Result;
import com.qidian.k12.vo.AdminQueryVo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * (Admin)表服务实现类
 *
 * @author makejava
 * @since 2021-10-28 17:57:23
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;
    @Resource
    private StringRedisTemplate redisTemplate;

    /**
     * token 用于校验
     * @param token
     * @return
     */
    @Override
    public K12Result checkLogin(String token) {
        K12Result result=K12Result.noFond();
        String key="user_"+token;
        //校验
        if (redisTemplate.hasKey(key)){
            result=K12Result.ok();
         String json=  redisTemplate.opsForValue().get(key);
         if (json!=null&& !"".equals(json)){
             Admin admin=JsonTools.string2Obj(json,Admin.class);
             result.setData(admin);
         }
         //更新时间
         redisTemplate.expire(key,30,TimeUnit.MINUTES);

        }
        return result;
    }

    /**
     * 登录成功  生成user_+token  放在redis里
     * @param adminName
     * @param adminPass
     * @return
     */
    @Override
    public K12Result login(String adminName, String adminPass) {
        K12Result result=K12Result.ok();
        adminPass= DigestUtils.md5DigestAsHex(adminPass.getBytes());
        Admin admin = adminMapper.queryByName(adminName);
        if (admin==null&&!admin.getAdminPass().equals(adminPass)){
            result.setStatus(K12Result.ResultStatus.ERROR);
            result.setMsg("用户名或密码错误");
            return result;
        }
        //用户UUID生成 token  用于checklogin校验
        String token= UUID.randomUUID().toString().replace("-","");
        //
        String key="user_"+token;
        //不显示密码
        admin.setAdminPass(null);

        redisTemplate.opsForValue().set(key, JsonTools.obj2String(admin));
        //设置存活时间
        redisTemplate.expire(key,30, TimeUnit.MINUTES);

        //数据返回
        Map map=new HashMap();

        map.put("admin",admin);
        //将token 传到前端
        map.put("token",token);

        result.setData(map);

        return result;
    }

    @Override
    public PageInfo<Admin> query(Map map) {
        System.out.println("map:"+map);
        PageInfo<Admin> pageInfo=new PageInfo();
        int page = new Integer(map.get("page").toString());
        int row = new Integer(map.get("row").toString());
        pageInfo.setPage(page);
        pageInfo.setRow(row);

        int total=adminMapper.total(map);
        pageInfo.setTotal(total);
        int maxPageNum=(int)(Math.ceil(total*1.0/row));
        pageInfo.setMaxPageNun(maxPageNum);
        //计算分页栏位置
        int pageStart=1;
        int pageEnd=10;
        if(page-4>1){
            pageStart=page-4;
        }
        if (page+5>maxPageNum){
            pageEnd=maxPageNum;
        }
        pageInfo.setPageStart(pageStart);
        pageInfo.setPageEnd(pageEnd);
        //数据 放在 pageInfo.data
       // adminMapper.query(map);
        pageInfo.setData(adminMapper.query(map));
        return pageInfo;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param adminId 主键
     * @return 实例对象
     */
    @Override
    public Admin queryById(Long adminId) {
        return this.adminMapper.queryById(adminId);
    }


    /**
     * 新增数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public K12Result insert(Admin admin) {
        K12Result result=K12Result.error();
        if (adminMapper.queryByName(admin.getAdminName())!=null){
            return  K12Result.dataEXists();
        }
        Date date=new Date();
        admin.setCreateTime(date);
        admin.setModifyTime(date);
        admin.setAdminStatus(1);
        if (adminMapper.insert(admin)==1){
            result=K12Result.ok();
        }
        return result;
    }

    /**
     * 修改数据
     *
     * @param admin 实例对象
     * @return 实例对象
     */
    @Override
    public K12Result update(Admin admin) {
        K12Result result=K12Result.error();
        admin.setModifyTime(new Date());
        if (adminMapper.update(admin)==1){
            result=K12Result.ok(this.queryById(admin.getAdminId()));
        }

        return result;
    }

    /**
     * 通过主键删除数据
     *
     * @param adminId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long adminId) {
        return this.adminMapper.deleteById(adminId) > 0;
    }
}
